package com.ruoyi.emergencyplan.service.impl;

import java.util.List;
import java.util.stream.Collectors;


import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONException;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.dangerControl.domain.SysDepart;
import com.ruoyi.emergencyplan.domain.EpEmergencyPlan;
import com.ruoyi.emergencyplan.dto.EpEscapeRouteDto;
import com.ruoyi.emergencyplan.mapper.EpEmergencyPlanMapper;
import com.ruoyi.emergencyplan.mapper.EpPlanSiteDisposalMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.emergencyplan.mapper.EpEscapeRouteMapper;
import com.ruoyi.emergencyplan.domain.EpEscapeRoute;
import com.ruoyi.emergencyplan.service.IEpEscapeRouteService;

/**
 * 应急预案——避灾路线管理Service业务层处理
 *
 * @author hcc
 * @date 2023-12-17
 */
@Service
public class EpEscapeRouteServiceImpl implements IEpEscapeRouteService
{
    @Autowired
    private EpEscapeRouteMapper epEscapeRouteMapper;
    @Autowired
    private EpEmergencyPlanMapper epEmergencyPlanMapper;
    @Autowired
    private EpPlanSiteDisposalMapper epPlanSiteDisposalMapper;
    /**
     * 查询应急预案——避灾路线管理
     *
     * @param escapeRouteId 应急预案——避灾路线管理主键
     * @return 应急预案——避灾路线管理
     */
    @Override
    public EpEscapeRoute selectEpEscapeRouteByEscapeRouteId(String escapeRouteId)
    {
        return epEscapeRouteMapper.selectEpEscapeRouteByEscapeRouteId(escapeRouteId);
    }

    /**
     * 查询应急预案——避灾路线管理列表
     *
     * @param epEscapeRoute 应急预案——避灾路线管理
     * @return 应急预案——避灾路线管理
     */
    @Override
    public List<EpEscapeRoute> selectEpEscapeRouteList(EpEscapeRoute epEscapeRoute)
    {
        return epEscapeRouteMapper.selectEpEscapeRouteList(epEscapeRoute);
    }

    /**
     * 新增应急预案——避灾路线管理
     *
     * @param epEscapeRoute 应急预案——避灾路线管理
     * @return 结果
     */
    @Override
    public int insertEpEscapeRoute(EpEscapeRoute epEscapeRoute)
    {
        return epEscapeRouteMapper.insertEpEscapeRoute(epEscapeRoute);
    }

    /**
     * 修改应急预案——避灾路线管理
     *
     * @param epEscapeRoute 应急预案——避灾路线管理
     * @return 结果
     */
    @Override
    public int updateEpEscapeRoute(EpEscapeRoute epEscapeRoute)
    {
        return epEscapeRouteMapper.updateEpEscapeRoute(epEscapeRoute);
    }

    /**
     * 批量删除应急预案——避灾路线管理
     *
     * @param escapeRouteIds 需要删除的应急预案——避灾路线管理主键
     * @return 结果
     */
    @Override
    public int deleteEpEscapeRouteByEscapeRouteIds(String[] escapeRouteIds)
    {
        return epEscapeRouteMapper.deleteEpEscapeRouteByEscapeRouteIds(escapeRouteIds);
    }

    /**
     * 删除应急预案——避灾路线管理信息
     *
     * @param escapeRouteId 应急预案——避灾路线管理主键
     * @return 结果
     */
    @Override
    public int deleteEpEscapeRouteByEscapeRouteId(String escapeRouteId)
    {
        return epEscapeRouteMapper.deleteEpEscapeRouteByEscapeRouteId(escapeRouteId);
    }

    /**
     * 避灾路线管理列表
     * @param epEscapeRoute
     * @return
     */
    @Override
    public List<EpEscapeRouteDto> getEpEscapeRouteList(EpEscapeRoute epEscapeRoute) {
        List<EpEscapeRoute> epEscapeRouteList = epEscapeRouteMapper.selectEpEscapeRouteList(epEscapeRoute);
        List<EpEscapeRouteDto> list = epEscapeRouteList.stream().map(epEscapeRoute1 -> {
            EpEscapeRouteDto epEscapeRouteDto = new EpEscapeRouteDto();
            BeanUtils.copyProperties(epEscapeRoute1, epEscapeRouteDto);
            // 应急预案ID
            List<String> planIds = epEscapeRouteMapper.getPlanIds(epEscapeRoute1.getEscapeRouteId());
            //应急预案
            List<EpEmergencyPlan> epEmergencyPlanList = planIds.stream().map(planId -> {
                EpEmergencyPlan epEmergencyPlan = epEmergencyPlanMapper.selectEpEmergencyPlanByPlanId(planId);
                return epEmergencyPlan;
            }).collect(Collectors.toList());
            // 现场处置预案管理ID
            List<String> disposalIds = epEscapeRouteMapper.getDisposalIds(epEscapeRoute1.getEscapeRouteId());
            // 现场处置预案管理
            /*List<EpPlanSiteDisposal> epPlanSiteDisposalList = disposalIds.stream().map(disposalId -> {
                EpPlanSiteDisposal epPlanSiteDisposal = epPlanSiteDisposalMapper.selectEpPlanSiteDisposalByPlanId(disposalId);
                return epPlanSiteDisposal;
            }).collect(Collectors.toList());*/
            List<EpEmergencyPlan> epPlanSiteDisposalList = disposalIds.stream().map(disposalId -> {
                EpEmergencyPlan epEmergencyPlan = epEmergencyPlanMapper.selectEpEmergencyPlanByPlanId(disposalId);
                return epEmergencyPlan;
            }).collect(Collectors.toList());
            List<String> orgInfos = epEscapeRouteMapper.getOrgInfos(epEscapeRoute1.getEscapeRouteId());
            // 机构信息
            List<SysDepart> orgList = orgInfos.stream().map(orgInfo -> {
                SysDepart sysDepart = new SysDepart();
                try {
                    JSONArray jsonArray = JSON.parseArray(orgInfo);
                    JSONObject jsonObject = jsonArray.getJSONObject(0);
                    sysDepart.setId(jsonObject.getString("id"));
                    sysDepart.setDepartName(jsonObject.getString("departName"));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                return sysDepart;
            }).collect(Collectors.toList());
            epEscapeRouteDto.setDisposalIds(disposalIds);
            epEscapeRouteDto.setPlanSiteDisposals(epPlanSiteDisposalList);
            epEscapeRouteDto.setPlanIds(planIds);
            epEscapeRouteDto.setPlans(epEmergencyPlanList);
            epEscapeRouteDto.setOrgList(orgList);
            return epEscapeRouteDto;
        }).collect(Collectors.toList());
        return list;
    }
}
